const request = require("request");
const cheerio = require("cheerio");
const mysql = require("mysql");
const db =mysql.createPool({
    host:"127.0.0.1",
    user:"root",
    database:"ykvideo",
    port:"3306",
    password:"123456"
});
function spider(url){
        request(url,(err,res,body)=>{
        if(err) console.log("请求出错");
        const $ = cheerio.load(body);
        const content = $("[name='list_card_unit']");
        const baseUrl = "https://ncxgg76.xyz";
        const baseImgUrl = "https://ncinci-asvmw.com"
        for(item of content) {
            const $item_a = cheerio.load(item);
            let aElemHref = baseUrl+$item_a("a").attr("href");
            let aElemTitle = $item_a("a").attr("title");
            let aElemTime = $item_a("a .videoBox_wrap .videoBox-time").text()
            const aElemImgContent = $item_a("a .videoBox_wrap script").text();
            let aElemUrl=aElemImgContent.slice(aElemImgContent.indexOf('getPicUrl("'),aElemImgContent.indexOf('");var')).replace('getPicUrl("',"").replace(".jpg","")
            let aElemImg = `${baseImgUrl}${aElemUrl}.jpg`
            let playerUrl = `https://ncpncp-tqcqg.com${aElemUrl}.m3u8`
            // console.log(`insert into videos(video_uri,video_title,video_img,video_time) values('${playerUrl}','${aElemTitle}','${aElemImg}','${aElemTime}')`)
            db.query(`insert into videos(video_uri,video_title,video_img,video_time) values('${playerUrl}','${aElemTitle}','${aElemImg}','${aElemTime}')`,function(err,result){
                if(!err) console.log("成功添加一条")
            })
        }
    })
}
let videolist=["https://ncxgg76.xyz/Html/tag/kongjie.html","https://ncxgg76.xyz/Html/tag/meinv.html","https://ncxgg76.xyz/Html/tag/qinglv.html","https://ncxgg76.xyz/Html/tag/yujie.html"]
for(j of videolist){
    spider(j)
}
